<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Update records based on a given criteria</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.--tostring.html">MongoCollection::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocollection.validate.html">MongoCollection::validate</a></div>
 <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongocollection.update" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoCollection::update</h1>
  <p class="verinfo">(PECL mongo &gt;=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCollection::update</span> &mdash; <span class="dc-title">Update records based on a given criteria</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongocollection.update-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><span class="type bool|array">bool|array</span></span> <span class="methodname"><strong>MongoCollection::update</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$criteria</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$new_object</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span>
  ] )</div>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongocollection.update-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">criteria</code></em>
     </dt>

     <dd>

      <p class="para">
       Description of the objects to update.
      </p>
     </dd>

    
    
     <dt>

      <em><code class="parameter">new_object</code></em>
     </dt>

     <dd>

      <p class="para">
       The object with which to update the matching records.
      </p>
     </dd>

    
    
     <dt>

      <em><code class="parameter">options</code></em>
     </dt>

     <dd>

      <p class="para">
       This parameter is an associative array of the form 
       <em>array(&quot;optionname&quot; =&gt; &lt;boolean&gt;, ...)</em>. Currently 
       supported options are: 
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="para">
          <em>&quot;upsert&quot;</em>
         </p>
         <p class="para">
          If no document matches <em><code class="parameter">$criteria</code></em>, a new
          document will be inserted.
         </p>
         <p class="para">
          If a new document would be inserted and
          <em><code class="parameter">$new_object</code></em> contains atomic modifiers
          (i.e. <em>$</em> operators), those operations will be
          applied to the <em><code class="parameter">$criteria</code></em> parameter to create
          the new document. If <em><code class="parameter">$new_object</code></em> does not
          contain atomic modifiers, it will be used as-is for the inserted
          document. See the upsert examples below for more information.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <em>&quot;multiple&quot;</em>
         </p>
         <p class="para">
          All documents matching $criteria will be updated.  
          <span class="function"><strong>MongoCollection::update()</strong></span> has exactly the opposite 
          behavior of <span class="function"><a href="mongocollection.remove.html" class="function">MongoCollection::remove()</a></span>: it updates 
          one document by default, not all matching documents.  <em class="emphasis">It is 
          recommended that you always specify whether you want to update 
           multiple documents or a single document</em>, as the database 
          may change its default behavior at some point in the future.
         </p>
        </li>
        <li class="listitem"><p class="para"><em>&quot;fsync&quot;</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. Forces the insert to be synced to disk before returning success. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and will override setting <em>w</em> to <em>0</em>.</p></li>
        <li class="listitem"><p class="para"><em>&quot;j&quot;</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. Forces the insert to be synced to the journal before returning success. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and will override setting <em>w</em> to <em>0</em>.</p></li>

        <li class="listitem"><p class="para"><em>&quot;w&quot;</em></p><p class="para">See <a href="mongo.writeconcerns.html" class="link">WriteConcerns</a>. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>1</em>.</p></li>
        <li class="listitem"><p class="para"><em>&quot;wtimeout&quot;</em></p><p class="para">How long to wait for <a href="mongo.writeconcerns.html" class="link">WriteConcern</a> acknowledgement. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>10000</em> milliseconds.</p></li>
        <li class="listitem"><p class="para"><em>&quot;safe&quot;</em></p><p class="para"><em class="emphasis">Deprecated</em>. Please use the <a href="mongo.writeconcerns.html" class="link">WriteConcern</a> <em>w</em> option.</p></li>
        <li class="listitem"><p class="para"><em>&quot;timeout&quot;</em></p><p class="para">Integer, defaults to <em>MongoCursor::$timeout</em>.  If &quot;safe&quot; is set, this sets how long (in milliseconds) for the client to wait for a database response.  If the database does not respond within the timeout period, a <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> will be thrown.</p></li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongocollection.update-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an array containing the status of the update if the
   <em>&quot;w&quot;</em> option is set. Otherwise, returns <strong><code>TRUE</code></strong>.
  </p>
  <p class="para">
   Fields in the status array are described in the documentation for
   <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongocollection.update-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">Throws <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> if the &quot;w&quot; option is set and the write fails.</p><p class="para">Throws <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> if the &quot;w&quot; option is set to a value greater than one and the operation takes longer than <var class="varname"><var class="varname">MongoCursor::$timeout</var></var> milliseconds to complete.  This does not kill the operation on the server, it is a client-side timeout. The operation in <em>MongoCollection::$wtimeout</em> is milliseconds.</p>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongocollection.update-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">

      <tr>
       <td>1.3.0</td>
       <td>
        The <em><code class="parameter">options</code></em> parameter no longer accepts a boolean
        to signify an upsert. Instead, this now has to be done with
        <em>array(&#039;upsert&#039; =&gt; true)</em>.
       </td>
      </tr>

      <tr>
       <td>1.2.11</td>
       <td>
        Emits <strong><code>E_DEPRECATED</code></strong> when
        <em><code class="parameter">options</code></em> is <span class="type"><span class="type scalar">scalar</span></span>.
       </td>
      </tr>

      <tr>
       <td>1.2.0</td>
       <td>Added <em>&quot;timeout&quot;</em> option.</td>
      </tr>

      <tr>
       <td>1.0.11</td>
       <td>
        Disconnects on &quot;not master&quot; errors if <em>&quot;safe&quot;</em> is set.
       </td>
      </tr>

      <tr>
       <td>1.0.9</td>
       <td>
        <p class="para">
         Added ability to pass integers to the <em>&quot;safe&quot;</em> option,
         which previously only accepted booleans.
        </p>
        <p class="para">
         Added <em>&quot;fsync&quot;</em> option.
        </p>
        <p class="para">
         The return type was changed to be an array containing error information
         if the <em>&quot;safe&quot;</em> option is used. Otherwise, a boolean
         is returned as before.
        </p>
       </td>
      </tr>

      <tr>
       <td>1.0.5</td>
       <td>Added <em>&quot;safe&quot;</em> option.</td>
      </tr>

      <tr>
       <td>1.0.1</td>
       <td>
        Changed <em><code class="parameter">options</code></em> parameter from boolean to array.
        Pre-1.0.1, the second parameter was an optional boolean value specifying
        an upsert.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongocollection.update-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1439">
   <p><strong>Example #1 <span class="function"><strong>MongoCollection::update()</strong></span></strong></p>
   <div class="example-contents"><p>
    Adding an address field to a document.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"lastname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Jones"&nbsp;</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$newdata&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'$set'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"address"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"1&nbsp;Smith&nbsp;Lane"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">$newdata</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#6 (0) {
  }
  [&quot;firstname&quot;]=&gt;
  string(3) &quot;Bob&quot;
  [&quot;lastname&quot;]=&gt;
  string(5) &quot;Jones&quot;
  [&quot;address&quot;]=&gt;
  string(12) &quot;1 Smith Lane&quot;
}
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1440">
   <p><strong>Example #2 <span class="function"><strong>MongoCollection::update()</strong></span> upsert examples</strong></p>
   <div class="example-contents"><p>
    Upserts can simplify code, as a single line can create the document if it
    does not exist (based on <em><code class="parameter">$criteria</code></em>), or update an
    existing document if it matches.
   </p></div>
   <div class="example-contents"><p>
    In the following example, <em><code class="parameter">$new_object</code></em> contains an
    atomic modifier. Since the collection is empty and upsert must insert a new
    document, it will apply those operations to the
    <em><code class="parameter">$criteria</code></em> parameter in order to create the document.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"uri"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"/summer_pics"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'$inc'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"page&nbsp;hits"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"upsert"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#9 (0) {
  }
  [&quot;uri&quot;]=&gt;
  string(12) &quot;/summer_pics&quot;
  [&quot;page hits&quot;]=&gt;
  int(1)
}
</pre></div>
   </div>
   <div class="example-contents"><p>
    If <em><code class="parameter">$new_object</code></em> does not contain atomic modifiers
    (i.e. <em>$</em> operators), upsert will use
    <em><code class="parameter">$new_object</code></em> as-is for the new document. This matches
    the behavior of a normal update, where not using atomic modifiers causes the
    document to be overwritten.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"joe"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"username"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"joe312"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"createdAt"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">()),&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"upsert"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#10 (0) {
  }
  [&quot;username&quot;]=&gt;
  string(6) &quot;joe312&quot;
  [&quot;createdAt&quot;]=&gt;
  object(MongoDate)#4 (0) {
  }
}
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1441">
   <p><strong>Example #3 <span class="function"><strong>MongoCollection::update()</strong></span> multiple example</strong></p>
   <div class="example-contents"><p>
    By default, <span class="function"><strong>MongoCollection::update()</strong></span> will only update
    the first document matching <em><code class="parameter">$criteria</code></em> that it
    finds. Using the &quot;multiple&quot; option can override this behavior, if needed.
   </p></div>
   <div class="example-contents"><p>
    This example adds a &quot;gift&quot; field to every person whose birthday is in the
    next day.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$today&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">'$lt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1&nbsp;day"</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"birthday"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$today</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'$set'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'gift'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$surprise</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"multiple"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongocollection.update-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   The <a href="mongo.updates.html" class="link">PHP documentation on updates</a> and the
   <a href="http://docs.mongodb.org/manual/tutorial/modify-documents/" class="link external">&raquo;&nbsp;MongoDB core docs</a>.
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.--tostring.html">MongoCollection::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocollection.validate.html">MongoCollection::validate</a></div>
 <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
